-- UISummonSelectTarget
-- Created by cheny3 Oct/26/2016
-- 选择目标界面

UISummonSelectTarget = class("UISummonSelectTarget", function()
    return cc.Layer:create();
end);

function UISummonSelectTarget.create(summon, skillId)
    return UISummonSelectTarget.new(summon, skillId);
end

function UISummonSelectTarget:ctor(summon, skillId)
    -- 初始化
    self:setName("UISummonSelectTarget");

    -- 复用魔藤模板
    local node = cc.CSLoader:createNode("layout/bine/BineSelectTarget.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.skillId = skillId;
    self.summon  = summon;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UISummonSelectTarget:resize()
    local node = self.node;
    local w = cc.Director:getInstance():getOpenGLView():getFrameSize().width;
    local h = cc.Director:getInstance():getOpenGLView():getFrameSize().height;

    local scaleX = w / DESIGN_WIDTH;
    local scaleY = h / DESIGN_HEIGHT;

    -- CT底部居中并缩放至合适大小
    AlignM.alignToBottomCenter(node, "CT");

    -- CT2顶部居中并缩放至合适大小
    AlignM.alignToTopCenter(node, "CT2");

    -- 蒙版需要拉伸至正常大小
    local CT = findChildByName(node, "CT");
    local CT2 = findChildByName(node, "CT2");
    local bg1 = findChildByName(node, "CT/bg1");
    local bg2 = findChildByName(node, "CT2/bg2");
    bg1:setScale(scaleX / CT:getScale(), scaleY / CT:getScale());
    bg2:setScale(scaleX / CT2:getScale(), scaleY / CT:getScale());

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UISummonSelectTarget:redraw()
    -- 显示遮罩
    self:showMask();

    -- 显示模型
    self:redrawModel();

    -- 技能效果
    local effect = findChildByName(self.node, "CT2/desc");
    local effectText = SkillM.getSkillDesc(self.skillId, nil, nil, {["source"] = self.summon});
    effectText = string.gsub(effectText, "%[blue]", "[light_green]");
    TextStyleM.setTextStyle(effect, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BROWN_YELLOW);
    applyString(effect, effectText);

    -- 提示信息
    local tipLabel = findChildByName(self.node, "CT/tip");
    TextStyleM.setTextStyle(tipLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BROWN_YELLOW);
    tipLabel:setString(getLocStr("select_target_tip"));

    -- 设置施法消耗
    local requireMp = SummonM.calcSkillRequireMp(self.summon, self.skillId);
    local costLabel = findChildByName(self.node, "CT/lanping/cost");
    TextStyleM.setTextStyle(costLabel, TextStyleM.TEXT_SIZE_MIDDLE2, TextStyleM.TEXT_COLOR_WHITE, true, -4);
    costLabel:setString(tostring(requireMp));

    -- 设置魔法值
    local curMp = self.summon:queryAttrib("mp");
    local curMpLabel = findChildByName(self.node, "CT/lanping/max")
    TextStyleM.setTextStyle(curMpLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_WHITE, true, -4);
    local str = string.format(getLocStr("item_count_format"), curMp);
    curMpLabel:setString(str);
end

-- 显示模型
function UISummonSelectTarget:redrawModel()
    -- 创建模型
    local classId = self.summon:query("class_id");
    local summonDisplay = SummonDisplay.create(classId);

    -- 设置缩放比例
    local scale = SummonM.query(classId, "model_scale");
    summonDisplay:setScale(scale);

    -- 设置偏移量
    local offsetX = SummonM.query(classId, "offset_x") or 0;
    local offsetY = SummonM.query(classId, "offset_y") or 0;

    summonDisplay:setPosition(tonumber(offsetX - 0), tonumber(offsetY - 10));

    local modelNode = findChildByName(self.node, "CT/model_node");
    modelNode:removeAllChildren();
    modelNode:addChild(summonDisplay);
end

-- 显示蒙版
function UISummonSelectTarget:showMask()
    showTargetSelectMask(self.targetType);
end

-- 隐藏蒙版
function UISummonSelectTarget:hideMask()
    hideTargetSelectMask();
end

-- 注册点击事件
function UISummonSelectTarget:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UISummonSelectTarget");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 注册事件处理回调函数
function UISummonSelectTarget:registerEventCallback()
    EventMgr.register("UISummonSelectTarget", event.TARGET_SELECTED, function(pos)
        print(pos);

        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        for i = 1, #openMonsterGrids do
            print(openMonsterGrids[i].index)
            if openMonsterGrids[i].index == pos then
                if self.targetType ~= ATTACKER then
                    -- 判断能否使用技能
                    local ret = SummonM.canCastSkill(self.summon, self.skillId);
                    if ret ~= true then
                        -- 提示无法使用
                        -- TODO
                    else
                        SummonM.useSkill(self.summon:getPos(), pos, self.skillId);
                    end
                end

                EventMgr.fire(event.PLAYER_MOVE, pos);
                break;
            end
        end

        self:hideMask();

        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UISummonSelectTarget");
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            self:hideMask();

            EventMgr.removeAll("UISummonSelectTarget");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif ev == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end




